package com.projectplace.api;

/*
 * Copyright (c) 2011 Projectplace International AB
 * Permission is hereby granted, free of charge, to any person 
 * obtaining a copy of this software and associated documentation 
 * files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, 
 * publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:The above copyright notice and this 
 * permission notice shall be included in all copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import com.projectplace.GUI.Image.ImageDownloader;
import com.projectplace.api.getInfoHandler;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;

public final class PPClient {
	
	private final ImageDownloader imageDownloader = new ImageDownloader();
	private static OAuthConsumer ppConsumer;
	SharedPreferences mSettings;
	
	public PPClient(OAuthConsumer ppConsumer){
		imageDownloader.setMode(ImageDownloader.Mode.CORRECT);
		PPClient.ppConsumer = ppConsumer;

	}
	
	public String signRequest(String url){
		try {
			return ppConsumer.sign(url);
		} catch (OAuthMessageSignerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthExpectationFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OAuthCommunicationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	
	 public static void sendRequest(String request,getInfoHandler callback){
		if(ppConsumer.getToken().length() != 0 && ppConsumer.getTokenSecret().length() != 0){
			Log.d("PPClient", "INNAN SIGN");	
			try {
				Log.d("ppclient",ppConsumer.sign(request));
				(new AsynchronousSender(new HttpGet(ppConsumer.sign(request)),callback)).start();
				/* 
				 * This is just for the application, that doesnt need the be shown for the user
				 */
			} catch (OAuthMessageSignerException e) { 
				// TODO Auto-generated catch block  
				callback.onFinish(null, new Error("402"));
				e.printStackTrace();
			} catch (OAuthExpectationFailedException e) {
				// TODO Auto-generated catch block
				callback.onFinish(null, new Error("403"));
				e.printStackTrace();
			} catch (OAuthCommunicationException e) {
				// TODO Auto-generated catch block
				callback.onFinish(null, new Error("404"));
				e.printStackTrace();
			}
			//This error needs to be shown to the user. Promting him/her to login again.
		}else
			callback.onFinish(null, new Error("401"));
	}
	
	 public static void postRequest(String request,String text,getInfoHandler callback){
			if(ppConsumer.getToken().length() != 0 && ppConsumer.getTokenSecret().length() != 0){
				Log.d("PPClient", "INNAN SIGN");	
				try {
					Log.d("ppclient",ppConsumer.sign(request));
					
					HttpPost tempPost = new HttpPost(request);
					List<NameValuePair> value=new ArrayList<NameValuePair>();
					value.add(new BasicNameValuePair("text",text));

					tempPost.setEntity(new UrlEncodedFormEntity(value));
					Log.d("PPCLIENT", tempPost.toString());
					ppConsumer.sign(tempPost);
					(new AsynchronousSender(tempPost,callback)).start();
					/* 
					 * This is just for the application, that doesnt need the be shown for the user
					 */
				} catch (OAuthMessageSignerException e) { 
					// TODO Auto-generated catch block  
					callback.onFinish(null, new Error("402"));
					e.printStackTrace();
				} catch (OAuthExpectationFailedException e) {
					// TODO Auto-generated catch block
					callback.onFinish(null, new Error("403"));
					e.printStackTrace();
				} catch (OAuthCommunicationException e) {
					// TODO Auto-generated catch block
					callback.onFinish(null, new Error("404"));
					e.printStackTrace();
				}
				/*catch (UnsupportedEncodingException e) {
					
					e.printStackTrace();
				}*/
				//This error needs to be shown to the user. Promting him/her to login again.
 catch (UnsupportedEncodingException e) {
					
					e.printStackTrace();
				}
			}else
				callback.onFinish(null, new Error("401"));
		}
	 
	 
	
	public void sendAvatarRequest(String url,String userID,ImageView view){
		imageDownloader.download(url+"/"+ppConsumer.getToken(),userID, view);
	}
}
